package com.su.dolphin.utils.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.su.dolphin.R;
import com.su.dolphin.utils.ViewHolderUtil;
import com.su.dolphin.utils.ViewUtil;

public class PopMenu
{
    private List<PopMenuItem> mItems;
    
    private Context context;
    private PopupWindow popupWindow;
    private ListView listView;
    
    public PopMenu(Context context) {
        this.context = context;
        init(ViewUtil.dip2px(165), LayoutParams.WRAP_CONTENT);
        
    }
    
    public PopMenu(Context context, int width, int height) {
        this.context = context;
        init(width, height);
    }
    
    void init(int width, int height) {
        mItems = new ArrayList<PopMenuItem>();
        View view = LayoutInflater.from(context).inflate(R.layout.layout_common_popmenu, null);
        listView = (ListView) view.findViewById(R.id.listView);
        listView.setAdapter(new PopAdapter());
        listView.setFocusableInTouchMode(true);
        listView.setFocusable(true);
        popupWindow = new PopupWindow(view, width, height);
        popupWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));//设置这句话, 点击外面才能关闭PopupWindow
        
    }
    
    public void setOnItemClickListener(final OnPopMenuItemClickListener listener) {
        listView.setOnItemClickListener(new OnItemClickListener()
        {
            
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mItems != null) {
                    listener.onItemClick(mItems.get(position));
                    dismiss();
                }
                
            }
        });
    }
    
    public interface OnPopMenuItemClickListener
    {
        
        void onItemClick(PopMenuItem item);
    }
    
    public List<PopMenuItem> getItems() {
        return mItems;
    }
    
    // 批量添加菜单项
    public void setItems(List<PopMenuItem> items) {
        mItems = items;
        ((BaseAdapter) listView.getAdapter()).notifyDataSetChanged();
    }
    
    public void toggle(View view) {
        if (popupWindow.isShowing()) {
            dismiss();
        }
        else {
            showAsDropDown(view);
        }
    }
    
    public void showAsDropDown(View parent) {
        showAsDropDown(parent, 0, -2);
        
    }
    
    public void showAsDropDown(View parent, int x, int y) {
        popupWindow.showAsDropDown(parent, x, y);
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        popupWindow.update();
    }
    
    // 隐藏菜单
    public void dismiss() {
        popupWindow.dismiss();
    }
    
    // 适配器
    private final class PopAdapter extends BaseAdapter
    {
        
        @Override
        public int getCount() {
            return mItems.size();
        }
        
        @Override
        public Object getItem(int position) {
            return mItems.get(position);
        }
        
        @Override
        public long getItemId(int position) {
            return position;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = ViewHolderUtil.inflateView(R.layout.layout_common_popmenu_item, context);
            }
            PopMenuItem popMenuItem = mItems.get(position);
            TextView popmenu_item_tv = ViewHolderUtil.findViewById(convertView, R.id.popmenu_item_tv);
            View popmenu_item_line = ViewHolderUtil.findViewById(convertView, R.id.popmenu_item_line);
            popmenu_item_tv.setText(popMenuItem.getTitle());
            if (popMenuItem.getResDrawable() > 0) {
                Drawable drawable = context.getResources().getDrawable(popMenuItem.getResDrawable());
                //int iconwh = ViewUtil.dip2px(27);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                popmenu_item_tv.setCompoundDrawables(drawable, null, null, null);
                popmenu_item_tv.setCompoundDrawablePadding(ViewUtil.dip2px(7));
            }
            if (position == getCount() - 1) {
                popmenu_item_line.setVisibility(View.INVISIBLE);
            }
            else {
                popmenu_item_line.setVisibility(View.VISIBLE);
            }
            return convertView;
        }
        
    }
    
    public static class PopMenuItem
    {
        private int id;
        private String title;
        private int resDrawable;
        
        public PopMenuItem(int id, String title, int drawable) {
            super();
            this.id = id;
            this.title = title;
            this.resDrawable = drawable;
        }
        
        public int getId() {
            return id;
        }
        
        public void setId(int id) {
            this.id = id;
        }
        
        public String getTitle() {
            return title;
        }
        
        public void setTitle(String title) {
            this.title = title;
        }
        
        public int getResDrawable() {
            return resDrawable;
        }
        
        public void setDrawable(int drawable) {
            this.resDrawable = drawable;
        }
        
    }
}
